iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 24
0
Software Development

30天的python學習 系列 第 24

python30天-DAY24-pandas-Series結構

  • 分享至 

  • xImage
  •  

今天來介紹 pandas 的另一個結構 Series
Series 是一個一維的陣列,裡面由index和value组成,其實 DataFrame 就是由多個 Series 所組成, DataFrame 裡面的任一行任一列都是一個 Series

我們可以自己創一個數據來演示。

import numpy as np
import pandas as pd
da = {
    "Name":["王小明","王中明","王大明","陳小明","陳大明"],
    "Age":["12","32",'34',"54","78"],
    "Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
dt

https://ithelp.ithome.com.tw/upload/images/20200924/20129601p4kH4WJ8lD.png

接著我們試試從 DataFrame 抓出一行數據出來。

import pandas as pd
import numpy as np
da = {
    "Name":["王小明","王中明","王大明","陳小明","陳大明"],
    "Age":["12","32",'34',"54","78"],
    "Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
SAge = pd.Series(dt["Age"])
print(SAge)
print(type(SAge))

我們把 DataFrame 裡的 Age 這一行取出來,接著我們把它打印出來,也順便把它的資料型態也印出來,結果如下圖。
https://ithelp.ithome.com.tw/upload/images/20200924/201296010FQbWYr07m.png
我們可以看到這五個人的Age都印出來了,接著看它的資料型態的確為 Series

接著我們來試試看把多個 Series 變成一個 DataFrame

我們把原本的 DataFrame 分成三個 Series(N,A,S) ,最後再把它合並起來

import pandas as pd
import numpy as np
da = {
    "Name":["王小明","王中明","王大明","陳小明","陳大明"],
    "Age":["12","32",'34',"54","78"],
    "Sex":["M","G","M","M","G"]
}
dt = pd.DataFrame(da)
N = pd.Series(dt["Name"])
A = pd.Series(dt["Age"])
S = pd.Series(dt["Sex"])
dd = pd.DataFrame([N,A,S],index=["Name","Age","Sex"]) #合併
dd = dd.T #因為創建完的DataFrame是顛倒的,所以用這個指令把它轉回來
print(N)
print(A)
print(S)
print(dd)
print(type(dd))

https://ithelp.ithome.com.tw/upload/images/20200924/20129601UaQXWJXE9D.png


上一篇
python30天-DAY23-pandas(9)
下一篇
python30天-DAY25-Matplotlib(1)
系列文
30天的python學習 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言